/**
 * 
 */
package org.myontology.controller.impl;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.Action;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.SesameSession;
import org.myontology.controller.pages.DisplayProperties;
import org.myontology.controller.pages.UserRedirectPage;
import org.myontology.users.Login;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
import org.openrdf.model.impl.LiteralImpl;
import org.openrdf.model.impl.URIImpl;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class Confirm extends AbstractAction implements Action {

	private SesameSession session;
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		DisplayProperties.HIDE_NAVIGATION.setValue(attributes, true);
		DisplayProperties.HIDE_TREE.setValue(attributes, true);
		if (!"init".equals(attributes.getRequestParameter("step"))) {
			String confirmCode = (String)attributes.getRequestParameter("confirmation");
			if (confirmCode!=null) {
				String userId = Login.CURRENT_USER.getValue(attributes);
				URI user = new URIImpl(Vocabulary.USER_NAMESPACE+userId);
				String checkCode = session.loadSingleObject(user, Vocabulary.HASLOCK);
				if (confirmCode.trim().equals(checkCode)) {
					session.remove(user, Vocabulary.HASLOCK, null);
					Literal sessionId = new LiteralImpl(attributes.getSessionId());
					session.update(user, Vocabulary.HASSESSIONID, sessionId);
					Login.LOGGED_IN.setValue(attributes, true);
					UserRedirectPage.REDIRECT.setValue(attributes, Login.REDIRECT_PATH.getValue(attributes));
				}
			}
		}

	}

}
